package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtils;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;

    /**
     * 登录
     * @param "emp"
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO emp){
        log.info("登录数据:{}",emp.toString());
        // 获取用户传入的用户和密码
        String username = emp.getUsername();
        String password = emp.getPassword();
        if(username == null || password == null){
            return Result.error("请输入账号或密码");
        }
        EmployeeLoginVO employee ;
        // 查询username
        try {
            employee = employeeService.login(username,password);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }


        return Result.success("登录成功",employee);
    }

    /**
     * 登出
     * @return
     */
    @ApiOperation("登出")
    @PostMapping("/logout")
    public Result<String> logout(){
        return Result.success("退出成功");
    }

    /**
     * 添加员工
     * @param emp
     * @return
     */
    @ApiOperation("添加员工")
    @PostMapping("/save")
    public Result<String> save(@RequestBody EmployeeDTO emp){
        log.info(emp.toString());
        employeeService.save(emp);
        return Result.success();
    }

    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO queryDTO){

        PageResult pageResult = employeeService.pageQuery(queryDTO);
        return Result.success("", pageResult);
    }
    @ApiOperation("员工状态设置")
    @GetMapping("/status")
    public Result statusChange(long id, Integer status){
        log.info("员工id:{}-status:{}",id,status);
        employeeService.statusChange(id,status);
        return Result.success();
    }

    @ApiOperation("根据id查询员工信息")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }
    @ApiOperation("根据id更新员工信息")
    @PutMapping("/update")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
    @ApiOperation("测试")
    @PostMapping("/test")
    public Result<String> test(){
        return Result.success("操作成功");
    }
}
